/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.x3.purchasing.inquery;

import com.x3.purchasing.common.ClassApplicationModule;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Div;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Vbox;

/**
 *
 * @author kris
 */
public class InqueryListWnd extends ClassApplicationModule {

    private InqueryDAO inqueryDAO;
    private Session session;
    private Grid gridInquery;
    private Div contentDiv;
    private Rows rows = new Rows();

    public void onCreate() throws SQLException, Exception {
        inqueryDAO = new InqueryDAOImpl();
        gridInquery = (Grid) getFellow("gridInquery");
        contentDiv = (Div) getFellow("contentDiv");
        this.loadInquery();
    }

    private void loadInquery() throws Exception {
        List<Inquery> inquerys = inqueryDAO.gets();
        if (gridInquery.getRows() != null) {
            gridInquery.getRows().getChildren().clear();
        }
        for (final Inquery inquery : inquerys) {           
            Row rowBody = new Row();    
            Label lblJudul = new Label(inquery.getJudul());
            lblJudul.setStyle("font-size:12px;font-weight:bold");   

            Vbox vbox = new Vbox();
            Toolbarbutton btnDetail = new Toolbarbutton();
            btnDetail.setTooltiptext("Add to cart");
            btnDetail.setStyle("font-size:9px;color:#0000FF;");
            btnDetail.setLabel("detail...");
            btnDetail.addEventListener("onClick", new EventListener() {

                public void onEvent(Event event) throws Exception {
                    session = Sessions.getCurrent();
                    session.setAttribute("idInquery", inquery.getId());
                    showObject("/zul/InqueryDetail.zul");
                }
            });
            Label lblBody = new Label();
            lblBody.setValue(inquery.getKeterangan());
            vbox.appendChild(lblJudul);
            vbox.appendChild(lblBody);
            vbox.appendChild(btnDetail);
            rowBody.appendChild(vbox);          
            rows.appendChild(rowBody);
            gridInquery.appendChild(rows);
        }        
    }

    public void showObject(String objName) throws Exception {
        contentDiv.getChildren().clear();
        Executions.createComponents(objName, contentDiv, null);
    }
}
